/**
 * @author JeffYoung
 * @create 2017年3月23日
 */
package com.scau.dao.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.scau.dao.BaseDao;
import com.scau.dao.IVipDao;
import com.scau.util.DateTransform;
import com.scau.vo.StoreInfo;
import com.scau.vo.Vip;

public class VipDaoImpl extends BaseDao implements IVipDao{
	
	private Session session = null;
	private Transaction tx = null;
	private String hql = "";
	
	/* (non-Javadoc)
	 * @see com.scau.dao.IVipDao#addVip(com.scau.vo.Vip)
	 */
	@Override
	public boolean saveVip(Vip vip) {
		session = getSession();
		tx = session.beginTransaction();
		
		/*HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("storeId");
		System.out.println("storeId为：" + id);
		Integer storeId = Integer.parseInt(id);*/
		
		/*String hql = "from StoreInfo si where si.id = '" + storeId + "'";
		StoreInfo store = (StoreInfo) session.createQuery(hql).uniqueResult();*/
		
		//插入带外键的数据。数据库中只有一条店铺记录
		hql = "from StoreInfo si";
		Query query = session.createQuery(hql);
		List<StoreInfo> siList = query.list();
		int id = siList.get(0).getId();
		StoreInfo store = (StoreInfo) session.get(StoreInfo.class, id);
		vip.setStoreInfo(store);
		
		session.save(vip);
		tx.commit();
		session.close();
		System.out.println("vip客户：" + vip.getName()+ "添加成功！");
		return true;
	}

	/* (non-Javadoc)
	 * @see com.scau.dao.IVipDao#delVip(int)
	 */
	@Override
	public boolean deleteVip(int id) {
		try {
			session = getSession();
			tx = session.beginTransaction();
			Vip vip = (Vip) session.get(Vip.class, id);
			session.delete(vip);
			tx.commit();
			session.close();
			System.out.println("vip客户：" + id + "删除成功！");
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("vip客户：" + id + "删除失败！");
			return false;
		}
	}

	/* (non-Javadoc)
	 * @see com.scau.dao.IVipDao#showVips()
	 */
	@Override
	public List<Vip> listVip() {
		session = getSession();
		hql = "from Vip vip";
		Query query = session.createQuery(hql);
		List<Vip> vips = query.list();
		
		session.close();
		return vips;
	}

	/* (non-Javadoc)
	 * @see com.scau.dao.IVipDao#selectVips(java.lang.String)
	 */
	@Override
	public List<Vip> selectVips(String message) {
		session = getSession();
		tx = session.beginTransaction();
		hql = "from Vip v where v.vipName like '%" + message
				+ "%'";
		Query query = session.createQuery(hql);
		List<Vip> list = query.list();
		System.out.println("查询出的list为：" + list);
		tx.commit();
		session.close();
		return list;
	}

	/* (non-Javadoc)
	 * @see com.scau.dao.IVipDao#updateVip(com.scau.vo.Vip)
	 */
	@Override
	public boolean updateVip(Vip vip) {
		session = getSession();
		tx = session.beginTransaction();
		
		Vip v = (Vip) session.get(Vip.class,vip.getId());
		
		//v.setConsumptionTime(vip.getConsumptionTime());
		v.setRemark(vip.getRemark());
		v.setName(vip.getName());
		v.setPhone(vip.getPhone());
		v.setSex(vip.getSex());
		v.setGmtModified(vip.getGmtModified());
		//外键
		/*HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("storeId");
		System.out.println("修改后的storeId为：" + id);
		Integer storeId = Integer.parseInt(id);
		String hql = "from StoreInfo si where si.id = '" + storeId + "'";
		StoreInfo store = (StoreInfo) session.createQuery(hql).uniqueResult();
		v.setStoreInfo(store);*/
	
		tx.commit(); 
		session.close();
		System.out.println("vip客户：" + vip.getName() + "更新成功！");
		return true;
	}
	
	// 根据ID查找一条Vip记录
	public List<Vip> getOneVip(int id){
		session = getSession();
		
		hql = "from Vip v where v.id = '" + id + "'";
		Query query = session.createQuery(hql);
		List<Vip> foundVip = query.list();
		
		session.close();
		return foundVip;
	}
	
	// 储值卡充值
	public List<Vip> rechargeForVip(Vip vip){
		session = getSession();
		tx = session.beginTransaction();
		
		hql = "from Vip v where v.id ='" + vip.getId() + "'";
		Query query = session.createQuery(hql);
		List<Vip> rechVip = query.list();
		// 原先的充值总金额及卡余额,待充值的金额。先从String转换成Int
		int originalRechAmount = Integer.parseInt(rechVip.get(0).getRechargeAmount());
		int originalAmount = Integer.parseInt(rechVip.get(0).getAmount());
		int amountForRech = Integer.parseInt(vip.getRechargeAmount());
		// 充值后的充值总金额和卡余额，转换回String
		String rAmount = originalRechAmount + amountForRech +"";
		String amount = originalAmount + amountForRech +"";
		// 充值操作
		rechVip.get(0).setRechargeAmount(rAmount);
		rechVip.get(0).setAmount(amount);
		// 判断是否改变会员等级
		int rechAmount = Integer.parseInt(rechVip.get(0).getRechargeAmount());
		if (rechAmount <= 1000) {
			rechVip.get(0).setType("普通会员");
		} else if (rechAmount <= 2000){
			rechVip.get(0).setType("银卡会员");
		} else if (rechAmount <= 3000){
			rechVip.get(0).setType("金卡会员");
		} else {
			rechVip.get(0).setType("黑卡会员");
		}
				
		tx.commit();
		session.close();
		return rechVip;
	}
	
	// 储值卡余额更新操作
	public List<Vip> changeVipAmount(Vip vip){
		session = getSession();
		tx = session.beginTransaction();
	
		hql = "from Vip v where v.id = '" + vip.getId() + "'";
		Query query = session.createQuery(hql);
		List<Vip> foundVip = query.list();
		
		// 储值卡本来余额及消费金额，消费后的余额
		int originalAmount = Integer.parseInt(foundVip.get(0).getAmount());
		int amountOfConsumption = Integer.parseInt(vip.getAmount());
		int afterAmount;
		
		if (originalAmount <= amountOfConsumption) {
			afterAmount = 0;
		} else {
			afterAmount = originalAmount - amountOfConsumption;
		}
		
		foundVip.get(0).setAmount(afterAmount+"");
		
		tx.commit();
		session.close();
		return foundVip;
	}
	
	
	

}
